package com.ocom.webapi.controller;


import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.entity.human.EntityDeptInfo;
import com.ocom.common.entity.payconfig.EntityXfMealLeave;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.GetOrderMealLeaveRequest;
import com.ocom.common.request.payconfig.NotMealRequest;
import com.ocom.common.request.ugcapi.GzhUserLeaveList;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.redis.util.RedisUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.CompanyClientService;
import com.ocom.webapi.feign.HumanClientService;
import com.ocom.webapi.feign.PayConfigClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
@Slf4j
public class XfMealLeaveController {

    @Autowired
    PayConfigClientService payConfigClientService;

    @Autowired
    HumanClientService humanClientService;

    @Autowired
    CompanyClientService companyClientService;

    @Resource
    private RedisUtil redisUtil;

    @PostMapping(PayConfigUrl.URL_getOrderMealLeave)
    public Result getOrderMealLeave (@RequestBody GetOrderMealLeaveRequest getOrderMealLeaveRequest){
        log.info("请假餐段查询（getOrderMealLeave）:{{}}",getOrderMealLeaveRequest);
        return payConfigClientService.getOrderMealLeave(getOrderMealLeaveRequest);
    }


    @PostMapping(PayConfigUrl.URL_setOrderMealLeave)
    public Result setOrderMealLeave(@RequestBody EntityXfMealLeave xfMealLeave){
        log.info("请假餐段设置(setOrderMealLeave):{{}}",xfMealLeave);
        return payConfigClientService.setOrderMealLeaveRequest(xfMealLeave);
    }

    @PostMapping(WebApiServiceUrl.Url_getLeaveApply)
    public Result getLeaveApply (@RequestBody GzhUserLeaveList getOrderMealLeaveRequest){
        log.info("乐东请假列表（getOrderMealLeave）:{{}}",getOrderMealLeaveRequest);
        getOrderMealLeaveRequest.setComId(SecurityUtils.getYoCiUser().getComId());
        return companyClientService.leavelApplyList(getOrderMealLeaveRequest);
    }

    @PostMapping(WebApiServiceUrl.Url_MealPerReportsOnShow)
    public Result MealPerReportsOnShow (@RequestBody NotMealRequest request){

        YoCiUser yoCiUser  = SecurityUtils.getYoCiUser();
        String dates = DateUtils.dateTime();
        //限流！
        String ipf = dates+":"+yoCiUser.getId().toString();
        Set<Object> rangeByScore = redisUtil.rangeByScore("consumeDetailOnLimit:"+ipf , System.currentTimeMillis() - 1000l * 60l, System.currentTimeMillis());
        redisUtil.expire("consumeDetailOnLimit:"+ipf,600l);
        if (rangeByScore != null &&  rangeByScore.size() > 15) {
            return ResultUtil.error(ResultEnum.Err_3118);
        }
        redisUtil.zAdd("consumeDetailOnLimit:"+ipf, System.currentTimeMillis() , System.currentTimeMillis());

        request.setComId(yoCiUser.getComId());
        if (request.getDeptId() != null && request.getDeptId() > 0) {
            Result<List<EntityDeptInfo>> listResule = humanClientService.getDeptList(request.getComId(), request.getDeptId().longValue(), 0);
            List<Long> deptIds = listResule.getData().stream().map(EntityDeptInfo::getDeptId).collect(Collectors.toList());
            request.setDeptIds(deptIds);
        }

        return payConfigClientService.getMealPerReports(request);
    }


}
